package com.css.zfzx.data.postgresql.qhProbabilityEarthquakeDangerPG.repository.impl;

import com.css.zfzx.data.postgresql.qhProbabilityEarthquakeDangerPG.repository.QhProbabilityEarthquakeDangerSuperRepository;
import com.css.zfzx.data.postgresql.qhProbabilityEarthquakeDangerPG.repository.entity.QhProbabilityEarthquakeDangerPostgresqlEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.Iterator;

/**
 * @Description: data
 * @Author: lhl
 * @CreateDate: 2021/3/23 14:14
 */
@Repository
@PropertySource("classpath:platform-config.yml")
public class QhProbabilityEarthquakeDangerSuperRepositoryImpl<T> implements QhProbabilityEarthquakeDangerSuperRepository<T> {
    @PersistenceContext
    private EntityManager em;
    @Value("${spring.jpa.properties.hibernate.jdbc.batch_size}")
    private int BATCH_SIZE;

    @Override
    public <S extends T> Iterable<S> batchSave(Iterable<S> var1) {
        Iterator<S> iterator = var1.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            em.persist(iterator.next());
            index++;
            if (index % BATCH_SIZE == 0) {
                em.flush();
                em.clear();
            }
        }
        if (index % BATCH_SIZE != 0) {
            em.flush();
            em.clear();
        }
        return var1;
    }

    @Override
    public  Iterable<QhProbabilityEarthquakeDangerPostgresqlEntity> batchSave2(Iterable<QhProbabilityEarthquakeDangerPostgresqlEntity> var1) {
        Iterator<QhProbabilityEarthquakeDangerPostgresqlEntity> iterator = var1.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            em.persist(iterator.next());
            index++;
            if (index % BATCH_SIZE == 0) {
                em.flush();
                em.clear();
            }
        }
        if (index % BATCH_SIZE != 0) {
            em.flush();
            em.clear();
        }
        return var1;
    }
}
